// stores/userStore.ts
import { defineStore } from 'pinia'
import type {MenuItem} from '@/interface/menu'

interface Role {
  roleName: string
  roleKey: string
}

interface User {
  createTime: string
  updateTime: string
  avatar: string | null
  creditScore: number
  deptId: number | null
  email: string
  isSeller: number | null
  lastLoginIp: string | null
  lastLoginTime: string | null
  nickName: string
  password: string
  phone: string
  realName: string | null
  status: string
  studentId: string | null
  universityId: number
  userId: number
  username: string
  token: string | null
  universityCode: string | null
}

interface LoginResponse {
  msg: string
  code: number
  isShowMsg: boolean
  roles: Role[]
  routerList: MenuItem[]
  user: User
  token: string
}

export const useUserStore = defineStore('user', {
  state: () => ({
    msg: '' as string,
    code: 0 as number,
    isShowMsg: false as boolean,
    roles: [] as Role[],
    menus: null as MenuItem[] | null,
    user: null as User | null,
    token: '' as string,
    routerCreated: false as boolean
  }),

  getters: {
    // 获取路由是否添加
    getRouterCreated: (state) => state.routerCreated,

    // 获取用户菜单列表
    getMenus: (state) => state.menus,

    // 获取角色列表
    getRoles: (state) => state.roles,
    
    // 获取特定角色
    getRoleByKey: (state) => (key: string) => {
      return state.roles.find(role => role.roleKey === key)
    },
    
    // 获取用户信息
    getUser: (state) => state.user,
    
    // 获取用户头像
    getAvatar: (state) => state.user?.avatar,
    
    // 获取用户创建时间
    getCreateTime: (state) => state.user?.createTime,
    
    // 获取用户信用分
    getCreditScore: (state) => state.user?.creditScore,
    
    // 获取用户部门ID
    getDeptId: (state) => state.user?.deptId,
    
    // 获取用户邮箱
    getEmail: (state) => state.user?.email,
    
    // 获取是否为卖家
    getIsSeller: (state) => state.user?.isSeller,
    
    // 获取最后登录IP
    getLastLoginIp: (state) => state.user?.lastLoginIp,
    
    // 获取最后登录时间
    getLastLoginTime: (state) => state.user?.lastLoginTime,
    
    // 获取用户昵称
    getNickName: (state) => state.user?.nickName,
    
    // 获取用户手机号
    getPhone: (state) => state.user?.phone,
    
    // 获取真实姓名
    getRealName: (state) => state.user?.realName,
    
    // 获取用户状态
    getStatus: (state) => state.user?.status,
    
    // 获取学号
    getStudentId: (state) => state.user?.studentId,
    
    // 获取大学ID
    getUniversityId: (state) => state.user?.universityId,
    
    // 获取更新时间
    getUpdateTime: (state) => state.user?.updateTime,
    
    // 获取用户ID
    getUserId: (state) => state.user?.userId,
    
    // 获取用户名
    getUsername: (state) => state.user?.username,
    
    // 获取JWT token
    getToken: (state) => state.token,
    
    // 判断是否已登录
    isLoggedIn: (state) => !!state.token && state.code === 200
  },

  actions: {
    // 设置登录数据
    setLoginData(data: LoginResponse) {
      this.msg = data.msg
      this.code = data.code
      this.isShowMsg = data.isShowMsg
      this.roles = data.roles
      this.user = data.user
      this.token = data.token
      // routerList 会在调用处单独处理
    },

    setMenuData(data:MenuItem[]){
      this.menus = data
    },

    setRouterStatus(status:boolean){
      this.routerCreated = status
    },
    
    // 清除用户数据（退出登录时使用）
    clearUserData() {
      this.msg = ''
      this.code = 0
      this.isShowMsg = false
      this.roles = []
      this.user = null
      this.token = '',
      this.menus = [],
      this.routerCreated = false
      // console.log('用户数据已清除')
    },
    
    // 更新用户信息
    updateUserInfo(userInfo: Partial<User>) {
      if (this.user) {
        this.user = { ...this.user, ...userInfo }
      }
    }
  },
  persist: true
})